<template>
  <div>
    <!-- 头部 --start-->
    <div class="nav">
      <p>
        <span>.</span>
        新增订单计划
      </p>

      <div id="nav_go5">
        <el-button
          type="info"
          icon="el-icon-back"
          class="btn_fanhui"
          @click="revert()"
        >返回
        </el-button
        >
      </div>
    </div>
    <!-- 头部 --end-->
    <!-- 合同信息 --start-->
    <div class="conter_box1" :data="listData">
      <p><em>1</em> 计划信息</p>
      <ul>
        <li>
          <span>企业名称</span>
          <template>
            <el-select
              placeholder="请输入客户名称"
              style="width: 70%"
              v-model="listData.enterpriseName"
              @change="GetProMiss($event)"
              value-key="id">
              <el-option
                v-for="item in clientIdarr"
                :key="item.key"
                :label="item.enterpriseName"
                :value="item"
              ></el-option>
            </el-select>
          </template>
        </li>
        <li>
          <span>联系人</span>
          <el-input
            placeholder="请输入联系人"
            v-model="listData.contacts"
            style="width: 70%"
            disabled
          ></el-input>
        </li>
        <li>
          <span>联系方式</span>
          <el-input
            v-model="listData.contactsPhone"
            placeholder="请输入联系方式"
            style="width: 70%"
            disabled
          >
          </el-input>
        </li>
        <li>
          <span>企业地址</span>
          <el-input
            v-model="listData.adress"
            placeholder="请输入企业地址"
            style="width: 70%"
            disabled
          ></el-input>
        </li>
        <li>
          <span>开户银行</span>
          <el-input
            v-model="listData.kaipiaoKaihuhang"
            placeholder="请选择开户银行"
            style="width: 70%"
            disabled
          >
          </el-input>
        </li>
        <li>
          <span>开户账号</span>
          <el-input
            v-model="listData.kaipiaoKaihuaccount"
            placeholder="请选择开户账号"
            style="width: 70%"
            disabled
          ></el-input>
        </li>
        <li>
          <span>纳税人识别号</span>
          <el-input
            placeholder="请输入纳税人识别号"
            v-model="listData.nashui"
            style="width: 70%"
            disabled
          ></el-input>
        </li>
      </ul>
    </div>
    <!-- 合同信息 --end-->

    <!-- 产品信息 --start-->
    <div class="conter_box3">
     <div class="conter_box3_header">
       <p><em>2</em> 产品信息</p>
       <el-button type="primary" size="small" @click="addDomain"
       >新增
       </el-button
       >
     </div>
      <template>
        <el-table :data="products" style="width: 100%" max-height="340px">
          <el-table-column label="产品图号" align="center">
            <template slot-scope="scope">
              <el-select
                v-model="scope.row.productTuhao"
                @change="GetProMess(scope.$index, $event)"
                value-key="id"
                placeholder="请选择状态"
              >
                <el-option
                  v-for="item in productLists"
                  :key="item.id"
                  :label="item.productTuhao"
                  :value="item"
                >
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="产品名称" align="center">
            <template slot-scope="scope">
              <el-input v-model="scope.row.productName" disabled></el-input>
            </template>
          </el-table-column>

          <el-table-column label="单价 (元)" align="center">
            <template slot-scope="scope">
              <el-input v-model="scope.row.price" disabled></el-input>
            </template>
          </el-table-column>
          <el-table-column label="单位" align="center">
            <template slot-scope="scope">
              <el-input v-model="scope.row.danwei" disabled></el-input>
            </template>
          </el-table-column>
<!--          <el-table-column label="税率" align="center">-->
<!--            <template slot-scope="scope">-->
<!--              <el-input v-model="scope.row.shuilv"></el-input>-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="产品数量" align="center">-->
<!--            <template slot-scope="scope">-->
<!--              <el-input v-model="scope.row.number"></el-input>-->
<!--            </template>-->
<!--          </el-table-column>-->
          <el-table-column label="需求数量" align="center">
            <template slot-scope="scope">
              <el-input v-model="scope.row.number" @input="jisuan($event,scope.$index,1)"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="小计 (元)" align="center">
            <template slot-scope="scope">
              <el-input v-model="scope.row.xiaoji"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="计划到货日期" align="center">
            <template slot-scope="scope">
              <el-date-picker
                v-model="scope.row.planTime"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
                style="width: 95%"
              >
              </el-date-picker>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">

              <el-button
                type="danger"
                icon="el-icon-delete"
                @click="resetForm(scope)"
                size="small"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </div>

    <div class="bottom">
      <el-button type="primary" size="medium" @click="Add()"
      >确定
      </el-button
      >
      <el-button type="info" size="medium" @click="revert()">返回</el-button>
    </div>
    <!-- 底部按钮 --end-->
  </div>
</template>
<script>
import axios from "axios";

export default {
  data() {
    return {
      listData: {
        enterpriseNumber: "",
        enterpriseName: "",
        contacts: "",
        contactsPhone: "",
        adress: "",
        id: "",
        kaipiaoKaihuhang: "",
        nashui: "",
        kaipiaoKaihuaccount: "",
        khId:"",
      },
      clientIdarr: [],
      productLists:[],
      products:[{}],
      clientId:"",
    };
  },
  created() {
    this.userId = JSON.parse(localStorage.getItem("message")).Id;
    this.userName = JSON.parse(localStorage.getItem("message")).Name;
    this.getquan()
  },
  methods: {
    accAdd(arg1,arg2) {
      var r1,r2,m;
      try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
      try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
      m=Math.pow(10,Math.max(r1,r2))
      return (arg1*m+arg2*m)/m
    },
    accMul(arg1, arg2) {
      var m = 0,s1 = arg1+"",s2 = arg2+"";
      try { m += s1.split(".")[1].length; } catch (e) {}
      try { m += s2.split(".")[1].length; } catch (e) {}
      return ( (Number(s1.replace(".", "")) * Number(s2.replace(".", ""))) / Math.pow(10, m) );
    },
    GetProMiss(e) {
      this.listData.enterpriseNumber = e.enterpriseNumber;
      this.listData.enterpriseName = e.enterpriseName;
      this.listData.contacts = e.contacts;
      this.listData.contactsPhone = e.contactsPhone;
      this.listData.adress = e.adress;
      this.listData.kaipiaoKaihuaccount = e.kaipiaoKaihuaccount;
      this.listData.kaipiaoKaihuhang = e.kaipiaoKaihuhang;
      this.listData.nashui = e.nashui;
      this.listData.khId = e.id;
      this.getCai(this.listData.khId)
    },
    GetProMess(ind, e) {
      this.products[ind].id = e.id;
      this.products[ind].productNumber = e.productTuhao;
      this.products[ind].productName = e.productName;
      this.products[ind].danwei = e.danwei;
      this.products[ind].price = e.price;
      // this.products[ind].shuilv = e.shuilv
    },

    // 获取所有客户资料
    getquan() {
      axios(
        "/JX_java/common/getClientMessageList?userId=" + this.userId
      ).then((res) => {
        let result = res.data.data;
        this.clientIdarr = result;
      });
    },
    // 获取供应商 产品
    getCai(id) {
      axios("/JX_java/common/getProductByclientId?clientId="+id).then((res) => {
        let result = res.data.data;
        this.productLists = result;
      });
    },
    //新增
    Add(id) {
      const arr = {
        clientName:this.listData.enterpriseName,
        clientId:this.listData.khId,
        createonUserid:this.userId,
        createonUsername:this.userName,
        products:[],
        clientMessage:{
          adress:this.listData.adress,
          enterpriseNumber:this.listData.enterpriseNumber,
          contacts:this.listData.contacts,
          contactsPhone:this.listData.contactsPhone,
          kaipiaoKaihuhang:this.listData.kaipiaoKaihuhang,
          nashui:this.listData.nashui,
          kaipiaoKaihuaccount:this.listData.kaipiaoKaihuaccount,
        },
      }

      this.products.forEach(e => {
        const product = {
          "danwei": "",
          "id": 0,
          "number": 0,
          "orderId": 0,
          "planTime": "",
          "price": 0,
          "productId": 0,
          "productName": "",
          "productNumber": "",
          "supplierId": 0,
          "type": 0,
          "xiaoji": 0
        }
        product.danwei = e.danwei;
        product.number = e.number;
        product.planTime = e.planTime
        product.price = e.price
        product.productName = e.productName
        product.productNumber = e.productNumber
        product.type = e.type
        product.xiaoji = e.xiaoji
        arr.products.push(product);
      })
      console.log(arr)
      // return
      axios
        .post(
          "/JX_java/sales/orderFrom/addSalesOrderPlan", arr
        )
        .then((res) => {
          if (res.data.data == "操作成功") {
            this.$message.success(res.data.data);
            this.$router.go(-1);
          } else {
            this.$message.error(res.data.data);
          }
        });
    },
    //计算
    jisuan(e,ind,typeid){

      if (typeid==1){
        if (!/^\d\d*$/.test(this.products[ind].number)) {
          this.products[ind].number = /^\d\d*!/.exec(this.products[ind].number);
        }
      }
      let total=0;
      for (let i = 0; i < this.products.length; i++) {
        let chengji=parseFloat(this.accMul(this.products[i].price,this.products[i].number));
        this.products[i].xiaoji=chengji
        total= this.accAdd(total,chengji);
      }
      this.contractMoney=total;
    },//计算总金额

    //新增采购-------动态添加
    addDomain() {
      var list = {
        productname: "",
      };
      this.products.push(list);
    },
    //新增采购-------动态删除
    resetForm(e) {
      this.products.splice(e.$index, 1);
    },
    dfg() {
    },
    handleSelect(item) {
    },
    handleIconClick(ev) {
    },

    //返回
    revert() {
      this.$router.go(-1);
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/scss/base.scss";

.ryuan {
  width: 20%;
  margin-top: 10px;
  padding-left: 1%;

  p {
    height: 40px;

    em {
      display: inline-block;
      width: 25px;
      height: 25px;
      text-align: center;
      line-height: 25px;
      border-radius: 50% 50%;
      border: solid 1px #ccc;
    }
  }
}

.nav {
  height: 60px;
  padding: 0 1%;
  position: relative;

  p {
    height: 40px;
    margin: 10px 0;
    line-height: 40px;

    span {
      background: $Header_color;
      color: $Header_color;
    }
  }

  #nav_go5 {
    position: absolute;
    top: 10px;
    right: 1%;

    img {
      height: 16px;
      padding-right: 10px;
    }
  }
}

.step {
  width: 50%;
  position: absolute;
  top: 0;
  left: 25%;
}

.conter_box1 {
  background: $white;
  height: 250px;
  width: 98%;
  padding: 5px 1% 0 1%;

  p {
    height: 40px;

    em {
      display: inline-block;
      width: 25px;
      height: 25px;
      text-align: center;
      line-height: 25px;
      border-radius: 50% 50%;
      border: solid 1px #ccc;
    }
  }

  ul {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    color: #909399;

    li {
      width: 33%;
      font-size: 14px;
      margin-bottom: 10px;
      text-align: center;
      span{
        display: inline-block;
        width: 25%;
      }
    }
  }
}

.color_box {
  width: 5px;
  height: 20px;
  background-color: $blue;
  position: absolute;
  top: 22px;
  left: 10px;
}

.conter_box2 {
  background: $white;
  height: 100px;
  width: 98%;
  margin-top: 10px;
  padding: 5px 1% 0 1%;

  p {
    height: 40px;

    em {
      display: inline-block;
      width: 25px;
      height: 25px;
      text-align: center;
      line-height: 25px;
      border-radius: 50% 50%;
      border: solid 1px #ccc;
    }
  }

  ul {
    width: 100%;
    display: flex;
    color: #909399;

    li {
      width: 20%;
      font-size: 14px;
      margin-bottom: 10px;
    }
  }
}

.conter_box3 {
  background: $white;
  width: 98%;
  height: 400px;
  margin-top: 10px;
  padding: 5px 1% 0 1%;
  .conter_box3_header {
    display: flex;
    justify-content:space-between;
    line-height: 40px;
    p {
      //width: 50%;
      height: 40px;
      em {
        display: inline-block;
        width: 25px;
        height: 25px;
        text-align: center;
        line-height: 25px;
        border-radius: 50% 50%;
        border: solid 1px #ccc;
      }
    }
    .el-button {
      height: 30px;
      margin-right: 20px;
      margin-top: 5px;
    }
  }


  // ul {
  //   width: 100%;
  //   display: flex;
  //   color: #909399;
  //   li {
  //     width: 20%;
  //     font-size: 14px;
  //     margin-bottom: 10px;
  //   }
  // }
}

// .dispose_box1 {
//   border: solid 1px #ccc;
// }
// .dispose_box2,
// .dispose_box3 {
//   border: solid 1px #ccc;
//   margin-top: 10px;
// }
.bottom {
  text-align: center;
  padding-top: 10px;
  padding-bottom: 10px;
  margin-top: 20px;
  // background: $white;
  position: relative;

  p {
    width: 20%;
    // height: 50px;
    line-height: 40px;
    position: absolute;
    top: 30px;
    left: 1%;
    display: flex;
    justify-content: space-between;
  }
}
</style>
<style>
.el-dialog__footer {
  text-align: center;
}

.el-input__inner {
  font-size: 12px;
  /* color: rgb(192, 196, 204); */
}

#conter_box5 .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

#conter_box5 .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

#conter_box5 .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 80px;
  height: 80px;
  line-height: 80px;
  text-align: center;
}

#conter_box5 .avatar {
  width: 80px;
  height: 80px;
  display: block;
}
</style>
